package listbyorder.access101_200.test123;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/8 13:37
 */
public class Solution2 {

    // 待补充 —————— 状态机
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2) return 0;

        int s1 = -prices[0];
        int s2 = Integer.MIN_VALUE;
        int s3 = Integer.MIN_VALUE;
        int s4 = Integer.MIN_VALUE;

        for (int i = 1; i < prices.length; i++) {
            s1 = Math.max(s1, -prices[i]);
            s2 = Math.max(s2, s1 + prices[i]);
            s3 = Math.max(s3, s2 - prices[i]);
            s4 = Math.max(s4, s3 + prices[i]);
        }

        return Math.max(0, s4);
    }

}
